package interfaces.spawnMenu.spawnMap;

import interfaces.GUISource;
import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.ListenerKeeperElement;

import java.io.IOException;

import logic.common.team.Team;
import map.spawnStation.SpawnPosition;
import map.spawnStation.SpawnStation;
import map.spawnStation.stationListeners.ConquerHelpedEvent;
import map.spawnStation.stationListeners.SpawnStationListener;
import map.spawnStation.stationListeners.StationConqueredEvent;

import org.fenggui.binding.render.Binding;
import org.fenggui.binding.render.ITexture;
import org.fenggui.binding.render.Pixmap;
import org.fenggui.decorator.background.PixmapBackground;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.util.Color;

import com.jme.system.DisplaySystem;

public class SpawnButtonContent extends MiniMapIcon {
	
	public enum SpawnType { MotherShip, SpawnStation, Squad, TDM }
	
	public static final String MINIMAP_PATH = "data/textures/gui/miniMap/";
	
	public static final String STATION_NORMAL_PATH = MINIMAP_PATH + "spawnPoint.png";
	public static final String STATIOM_SELECTED_PATH = MINIMAP_PATH + "spawnPointSelected.png";
	public static final String STATION_NEUTRAL_PATH = MINIMAP_PATH + "spawn_neutral_icon.png";
	public static final String STATION_ENEMY_PATH = MINIMAP_PATH + "spawn_enemy_icon.png";
	public static final int STATION_WIDTH_FACTOR = 30;
	public static final int STATION_HEIGHT_FACTOR = STATION_WIDTH_FACTOR;
	
	private static final String MS_NORMAL_PATH = MINIMAP_PATH + "normalIcon.png";
	private static final String MS_SELECTED_PATH = MINIMAP_PATH + "selectedIcon.png";
	private static final int MS_WIDTH_FACTOR = 20;
	private static final int MS_HEIGHT_FACTOR = MS_WIDTH_FACTOR;
	
	private static final String SQUAD_NORMAL_PATH = MINIMAP_PATH + "hunterIconSquad.png";
	private static final String SQUAD_SELECTED_PATH = MINIMAP_PATH + "hunterIconSquadSelected.png";
	private static final int SQUAD_WIDTH_FACTOR = 50;
	private static final int SQUAD_HEIGHT_FACTOR = SQUAD_WIDTH_FACTOR;
		
	private SpawnPosition spawnPos;
	private SpawnButton button;
	private SpawnStationListener stationListener;
	private Team team;
	
	public SpawnButtonContent(SpawnPosition spawnPos, Team team, ListenerKeeperElement element) {
		super(0, 0, 0, 0);
		this.spawnPos = spawnPos;
		this.team = team;
		
		SpawnType type = spawnPos.getSpawnType();
		
		button = new SpawnButton(this);
		addWidget(button);
		
		String normal = null;
		String selected = null;
		
		DisplaySystem display = DisplaySystem.getDisplaySystem();
		int width = display.getWidth();
		int height = display.getWidth();
		
		switch(type) {
			case MotherShip: normal = MS_NORMAL_PATH;
							 selected = MS_SELECTED_PATH;
							 width /= MS_WIDTH_FACTOR;
							 height /= MS_HEIGHT_FACTOR;
							 break;
			case SpawnStation:  SpawnStation station = (SpawnStation)spawnPos;
								Color labelColor = GUISource.green;
								
								button.setText(station.getStationLetter());
								
								Team stationTeam = station.getTeam();
								button.setEnabled(false);
								if(stationTeam == null) {
									normal = STATION_NEUTRAL_PATH;
									labelColor = Color.GRAY;
								} else if(stationTeam != team) {
									normal = STATION_ENEMY_PATH;
									labelColor = Color.RED;
								}
								
								selected = normal;
								if(stationTeam == team) {
									normal = STATION_NORMAL_PATH;
									selected = STATIOM_SELECTED_PATH;
									button.setEnabled(true);
									labelColor = GUISource.green;
								}
								
								GUISource.setTextColor(button, labelColor);
								
								stationListener = getSpawnStationListener();
								element.addListener(station, stationListener);
								assert(station.getListenerContainer(SpawnStationListener.class).containsListener(stationListener));
								
								width /= STATION_WIDTH_FACTOR;
								height /= STATION_HEIGHT_FACTOR;
								
								break;
			case Squad: normal = SQUAD_NORMAL_PATH;
						selected = SQUAD_SELECTED_PATH;
						width /= SQUAD_WIDTH_FACTOR;
						height /= SQUAD_HEIGHT_FACTOR;
						break;
			case TDM: 	normal = STATION_NORMAL_PATH;
						selected = STATIOM_SELECTED_PATH;
						width /= STATION_WIDTH_FACTOR;
						height /= STATION_HEIGHT_FACTOR;
						break;
		}
		
		setSize(width, height);
		button.setSize(width, height);
		button.init(normal, selected);
		
		layout();
	}
	
	protected SpawnStationListener getSpawnStationListener() {
		return new SpawnStationListener() {
			@Override
			public void conquered(StationConqueredEvent event) {
				if(event.getConquerer().getTeam() == team) {
					button.init(STATION_NORMAL_PATH, STATIOM_SELECTED_PATH);
					button.setEnabled(true);
					GUISource.setTextColor(button, GUISource.green);
				} else {
					button.init(STATION_ENEMY_PATH, STATION_ENEMY_PATH);
					button.setEnabled(false);
					GUISource.setTextColor(button, Color.RED);
				}
				layout();
			}

			@Override
			public void stationNeutralized() {
				button.init(STATION_NEUTRAL_PATH, STATION_NEUTRAL_PATH);
				button.setEnabled(false);
				GUISource.setTextColor(button, Color.GRAY);
				layout();
			}
			
			@Override
			public void helpedConquer(ConquerHelpedEvent event) {}
		};
	}
	
	public SpawnType getType() { return spawnPos.getSpawnType(); }
	
	public void setSelected(boolean sel) { button.changeSelected(sel); }
	
	public boolean isSelected() { return button.isSelected(); }
	
	public SpawnPosition getSpawnPosition() { return spawnPos; }
	
	public void addButtonPressedListener(IButtonPressedListener l) {
		button.addButtonPressedListener(l);
	}
		
	public void clearStationListener() {
		if(spawnPos instanceof SpawnStation && stationListener != null) {
			SpawnStation station = (SpawnStation)spawnPos;
			station.removeSpawnStationListener(stationListener);
		}
	}
	
	
	public class SpawnButton extends InterfaceButton {
		
		private SpawnButtonContent content;
		private PixmapBackground normal, selected;
		private boolean isSelected;
		
		public SpawnButton(SpawnButtonContent content) {
			super(GUISource.middleFont);
			this.content = content;
		}
		
		public boolean isSelected() { return isSelected; }

		public void init(String normalPath, String selectedPath) {
			try {
				ITexture texture = Binding.getInstance().getTexture(normalPath);
				Pixmap pixmap = new Pixmap(texture);
				normal = new PixmapBackground(pixmap, true);
				
				texture = Binding.getInstance().getTexture(selectedPath);
				pixmap = new Pixmap(texture);
				selected = new PixmapBackground(pixmap, true);
				
				changeSelected(false);
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
		
		public void changeSelected(boolean sel) {
			isSelected = sel;
			
			getAppearance().removeAll();
			
			PixmapBackground bg = (sel ? selected : normal);
			
			getAppearance().add(bg);
			
			layout();
		}
		
		public SpawnButtonContent getSpawnButtonContent() { return content; }
	}
}